package net.lab1024.sa.admin.module.business.erp.poo.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.admin.module.business.erp.poo.dao.StoordabackDao;
import net.lab1024.sa.admin.module.business.erp.poo.dao.SubordabackDao;
import net.lab1024.sa.admin.module.business.erp.poo.dao.SubordbackDao;
import net.lab1024.sa.admin.module.business.erp.poo.domain.entity.StoordabackEntity;
import net.lab1024.sa.admin.module.business.erp.poo.domain.entity.SubordabackEntity;
import net.lab1024.sa.admin.module.business.erp.poo.domain.entity.SubordbackEntity;
import net.lab1024.sa.admin.module.business.erp.poo.domain.form.SubordbackAddForm;
import net.lab1024.sa.admin.module.business.erp.poo.domain.form.SubordbackQueryForm;
import net.lab1024.sa.admin.module.business.erp.poo.domain.form.SubordbackUpdateForm;
import net.lab1024.sa.admin.module.business.erp.poo.domain.vo.SubordabackVO;
import net.lab1024.sa.admin.module.business.erp.poo.domain.vo.SubordbackVO;
import net.lab1024.sa.admin.module.business.erp.inv.domain.vo.TargetVo;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 暂收送检单 Service
 *
 * @Author AKITA
 * @Date 2025-08-03 23:56:11
 * @Copyright AKITA
 */

@Service
public class SubordbackService {

    @Resource
    private SubordbackDao subordbackDao;
    @Resource
    private SubordabackDao subordabackDao;
    @Resource
    private StoordabackDao stoordabackDao;

    /**
     * 分页查询
     */
    public PageResult<SubordbackVO> queryPage(SubordbackQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<SubordbackVO> list = subordbackDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 添加
     */
    @Transactional
    public ResponseDTO<Long> add(SubordbackAddForm addForm) {
        SubordbackEntity subordbackEntity = SmartBeanUtil.copy(addForm, SubordbackEntity.class);
        subordbackDao.insert(subordbackEntity);
        for (SubordabackEntity subordabackEntity : addForm.getLista()) {
            subordabackEntity.setId(subordbackEntity.getId());
            subordabackDao.insert(subordabackEntity);
        }
        return ResponseDTO.ok(subordbackEntity.getId());
    }

    /**
     * 更新
     */
    public ResponseDTO<String> update(SubordbackUpdateForm updateForm) {
        SubordbackEntity subordbackEntity = SmartBeanUtil.copy(updateForm, SubordbackEntity.class);
        subordbackDao.updateById(subordbackEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return ResponseDTO.ok();
        }

        subordbackDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Long id) {

        subordbackDao.deleteById(id);
        subordabackDao.deleteById(id);
        return ResponseDTO.ok();
    }

    public ResponseDTO<SubordbackVO> info(Long id) {
        SubordbackEntity subordbackEntity = subordbackDao.selectById(id);
        SubordbackVO copy = SmartBeanUtil.copy(subordbackEntity, SubordbackVO.class);
        List<SubordabackEntity> subordabackEntities = subordabackDao.selectList(new LambdaQueryWrapper<SubordabackEntity>().eq(SubordabackEntity::getId, id));
        List<SubordabackVO> subordabackVOS = SmartBeanUtil.copyList(subordabackEntities, SubordabackVO.class);
        ArrayList<TargetVo> targetVob = new ArrayList<>();
        for (SubordabackEntity item : subordabackEntities) {
            if (item.getBaseId() != null) {
                TargetVo targetVo = new TargetVo();
                targetVo.setId(item.getBaseId());
                targetVo.setLineNum(item.getBaseLineNum());
                targetVo.setItemId(item.getItemId());
                targetVo.setItemName(item.getItemName());
                targetVo.setQty(item.getQty());
                targetVob.add(targetVo);
            }
        }

        List<StoordabackEntity> stoordabackEntities = stoordabackDao.selectList(new LambdaQueryWrapper<StoordabackEntity>().eq(StoordabackEntity::getBaseId, id));


        ArrayList<TargetVo> targetVoc = new ArrayList<>();
        for (StoordabackEntity item : stoordabackEntities) {
            TargetVo targetVo = new TargetVo();
            targetVo.setId(item.getId());
            targetVo.setLineNum(item.getLineNum());
            targetVo.setItemId(item.getItemId());
            targetVo.setItemName(item.getItemName());
            targetVo.setQty(item.getQty());
            targetVoc.add(targetVo);
        }



        copy.setLista(subordabackVOS);
        copy.setListb(targetVob);
        copy.setListc(targetVoc);
        return ResponseDTO.ok(copy);
    }

    public ResponseDTO<SubordbackVO> infoUnexecute(Long id) {
        SubordbackEntity subordbackEntity = subordbackDao.selectById(id);
        SubordbackVO copy = SmartBeanUtil.copy(subordbackEntity, SubordbackVO.class);
        List<SubordabackVO> subordabackVOS = subordabackDao.infoUnexecute(id);
        copy.setLista(subordabackVOS);
        return ResponseDTO.ok(copy);

    }
}
